package com.example.shop_2304.minefg

import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.PopupWindow
import android.widget.TextView
import androidx.appcompat.app.ActionBar.LayoutParams
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseFragment
import com.example.base.param.PRefund
import com.example.shop_2304.R
import com.example.shop_2304.adapter.OrderAdapter
import com.example.shop_2304.adapter.OrderRoomAdapter
import com.example.shop_2304.databinding.FragmentPay2Binding
import com.example.shop_2304.databinding.FragmentPay3Binding
import com.example.shop_2304.room.AppDataBase
import com.example.shop_2304.room.AppDbUtils
import com.example.shop_2304.room.OrderRoom


class Pay3Fragment  : BaseFragment<FragmentPay3Binding, PayViewModel>() {
    lateinit var orderAdapter: OrderRoomAdapter
    override fun initView() {
        orderAdapter = OrderRoomAdapter()
        binding.rv.adapter = orderAdapter
        binding.rv.layoutManager = LinearLayoutManager(context)

        val query = AppDbUtils.getDb().getOrderDao().query("待收货")
        orderAdapter.data.clear()
        orderAdapter.data.addAll(query)
        orderAdapter.notifyDataSetChanged()
        orderAdapter.addChildClickViewIds(R.id.order_tv2,R.id.order_tv)
        orderAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.order_tv2->{
                    //弹出选择框
                    val popupWindow = PopupWindow(context)
                    popupWindow.width = LayoutParams.MATCH_PARENT
                    popupWindow.height = LayoutParams.WRAP_CONTENT
                    val view1 = LayoutInflater.from(context).inflate(R.layout.item_refund, null)
                    view1.findViewById<TextView>(R.id.tv_amount).text = "退款金额:${orderAdapter.data[position].payPrice}"
                    view1.findViewById<TextView>(R.id.tv1).setOnClickListener {
                        view1.findViewById<TextView>(R.id.tv1).setTextColor(Color.RED)
                        view1.findViewById<TextView>(R.id.tv2).setTextColor(Color.BLACK)
                    }
                    view1.findViewById<TextView>(R.id.tv2).setOnClickListener {
                        view1.findViewById<TextView>(R.id.tv2).setTextColor(Color.RED)
                        view1.findViewById<TextView>(R.id.tv1).setTextColor(Color.BLACK)
                    }
                    view1.findViewById<Button>(R.id.btn).setOnClickListener {
                        val pRefund = PRefund()
                        pRefund.id = orderAdapter.data[position].id
                        pRefund.uni = orderAdapter.data[position].orderId
                        viewModel.refund(pRefund)

                        orderAdapter.data.removeAt(position)
                        orderAdapter.notifyItemRemoved(position)

                        popupWindow.dismiss()
                    }
                    popupWindow.contentView = view1
                    popupWindow.isOutsideTouchable = true
                    popupWindow.isFocusable = true
                    popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0)
                }
                R.id.order_tv->{
                    //先吐司，保存数据库  页面删除
                    ToastUtils.showLong("确认收货成功")
                    orderAdapter.data[position].orderStatus = "待评价"
                    AppDbUtils.getDb().getOrderDao().update(orderAdapter.data[position])

                    orderAdapter.data.removeAt(position)
                    orderAdapter.notifyItemRemoved(position)
                }
            }
        }
    }

    override fun initData() {

    }

    override fun getLayout(): Int = R.layout.fragment_pay3

    override fun getViewModel(): Class<PayViewModel>  = PayViewModel::class.java

}